package com.itheima.reggie.controller;



import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
 @Autowired
 private ShoppingCartService shoppingCartService;

 @PostMapping("/add")
 @CacheEvict(value = "shoppingCart",allEntries = true)
 public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

  R<ShoppingCart> add = shoppingCartService.add(shoppingCart);

  return add;
 }

 @PostMapping("/sub")
 @CacheEvict(value = "shoppingCart",allEntries = true)
 public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {

  R<ShoppingCart> sub = shoppingCartService.sub(shoppingCart);

  return sub;
 }

 @GetMapping("/list")
 @Cacheable(value = "shoppingCart" )
 public R<List<ShoppingCart>> list() {

  R<List<ShoppingCart>> listShoppingCart = shoppingCartService.list1();

  return listShoppingCart;
 }

 @DeleteMapping("/clean")
 @CacheEvict(value = "shoppingCart",allEntries = true)
 public R<String> delete() {
  log.info("清空购物车成功");

  shoppingCartService.delete();

  return R.success("清空成功");
 }

}
